<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务2</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }

        #box{
            width: 400px;
            border: 1px solid #000;
            margin: 100px auto;
        }

        #tab{
            display: flex;
            overflow: hidden;
            background: #333;
        }

        #tab li{
            flex: 1;
            list-style: none;
            height: 30px;
            line-height: 30px;
        }

        #tab li a{
            text-decoration: none;
            padding: 0 25px;
            color: aliceblue;
        }

        #tab li.sec a {
            color: #0ae9e9;
        }

        #content{
            width: 100%;
            height: 300px;
            background: #f2f2f2;
        }
        
        #content > div{
            display: none;
            line-height: 150px;
            text-align: center;
        }

        #content > div.active{
            display: block;
        }


    </style>
</head>
<body>
    <div id="box">
        <ul id="tab">
            <li class="sec"><a href="#">新闻</a></li>
            <li><a href="#">音乐</a></li>
            <li><a href="#">电影</a></li>
        </ul>
        <div id="content">
            <div class="active"><h1>新闻内容</h1></div>
            <div><h1>音乐内容</h1></div>
            <div><h1>电影内容</h1></div>
        </div>
    </div>

    <script>
        
        var tab = document.getElementById("tab");

        var li = document.getElementsByTagName("li");

        var content = document.getElementById("content");

        var div = content.getElementsByTagName("div");

        var index = 0;//表示当前选中的选项卡
        for(let i=0;i<li.length;i++){
            li[i].onclick = function(){
                if(i !== index){
                    div[i].className = "active";
                    li[i].className = "sec";

                    div[index].className = "";
                    li[index].className = "";

                    index = i;//更新为新选择的选项卡
                }                              
            }
        }
    </script>
</body>
</html>